//
//  HCreatImage.m
//  HTanzhi
//
//  Created by 陈贵 on 15/11/20.
//  Copyright © 2015年 hzj. All rights reserved.
//

#import "HCreatImage.h"

@interface HCreatImage ()<UITextFieldDelegate>
//二维码显示图
@property (nonatomic ,strong) UIImageView *imageV;

//文本框
@property (nonatomic ,strong) UITextField *textField;
//生成二维码的按钮
@property (nonatomic ,strong) UIButton *creatBth;


@end

@implementation HCreatImage

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUI];
//    [self creatTheImage];
}

///图片显示框
- (void) setUI{
    //文本
    UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(10, 10, 150, 30)];
    textField.backgroundColor = [UIColor grayColor];
    textField.delegate = self;
    
    textField.placeholder = @"请输入内容";
    self.textField = textField;
    [self.view addSubview:textField];
    //按钮
    UIButton *bth = [UIButton buttonWithType:UIButtonTypeCustom];
    bth.frame = CGRectMake(CGRectGetMaxX(_textField.frame), _textField.frame.origin.y, 100, 30);
    [bth setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [bth setTitle:@"生成二维码" forState:UIControlStateNormal];
    [bth addTarget:self action:@selector(creatTheImage) forControlEvents:UIControlEventTouchUpInside];
    self.creatBth = bth;
    [self.view addSubview:bth];
    
    //图片框
    UIImageView *iv = [[UIImageView alloc]initWithFrame:CGRectMake( 0 ,0, 150, 150)];
    iv.center = self.view.center;
    self.imageV = iv;
    [self.view addSubview:iv];
    
}

- (void) creatTheImage{
    //退出键盘
    [self.textField resignFirstResponder];
    //建立滤镜
    CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    //重置初始值
    [filter setDefaults];
    
    //设置内容
    NSString *str = self.textField.text;
    NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
    [filter setValue:data forKey:@"inputMessage"];
    
    CIImage *ciImage = filter.outputImage;
    
    //建立过滤单色的滤镜
    CIFilter *colorF = [CIFilter filterWithName:@"CIFalseColor"];
    [colorF setDefaults];
    [colorF setValue:ciImage forKey:@"inputImage"];
    
    //二维码颜色
    [colorF setValue:[CIColor colorWithRed:0 green:0 blue:0 alpha:1] forKey:@"inputColor0"];
    //二维码背景色
    [colorF setValue:[CIColor colorWithRed:1 green:1 blue:1 alpha:1] forKey:@"inputColor1"];
    CGAffineTransform transform = CGAffineTransformMakeScale(5, 5);
    CIImage *ciImage1 = [colorF.outputImage imageByApplyingTransform:transform];
    
    UIImage *image = [UIImage imageWithCIImage:ciImage1];
    UIImage *icon = [UIImage imageNamed:@"icon114x114"];
    
    self.imageV.image = [self getImage:image andIconImage:icon];
    
    
}
- (UIImage *)getImage:(UIImage *)bacImage andIconImage:(UIImage *)iconImage{
    
    CGSize size = bacImage.size;
    
    UIGraphicsBeginImageContext(size);
    //绘制背景图
    [bacImage drawInRect:CGRectMake(0, 0, size.width, size.height)];
    //绘制头像
    CGFloat w = size.width  * 0.25;
    CGFloat h = size.height * 0.25;
    CGFloat x = ( size.width - w ) * 0.5;
    CGFloat y = ( size.height - w ) * 0.5;
    [iconImage drawInRect:CGRectMake(x, y, w, h)];
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}

- (void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    //退出键盘
    [self.textField resignFirstResponder];
    
}


@end
